<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_Model extends CI_Model 
{
	public function __construct()
        {
            // Call the Model constructor
            parent::__construct();
            $this->load->helper('date');
            timezones('UP7');
        }
	
	function _required($required, $data)
	{
		foreach($required as $field)
			if(!isset($data[$field])) return false;
			
		return true;
	}
	
	function _default($defaults, $options)
	{
		return array_merge($defaults, $options);
	}
	
	
	function user_add($options = array())
	{
		// required values
		if(!$this->_required( array('user_email', 'user_password'), $options) ) return false;
		
		$options = $this->_default(array('user_status' => 1), $options);
		
		$this->db->insert('users', $options);
		
		return $this->db->insert_id();
	}
	
	
	function user_update($options = array())
	{
		// required values
		if(!$this->_required( array('user_id'), $options) ) return false;

		if(isset($options['user_email'])) $this->db->set('user_email', $options['user_email']);

		if(isset($options['user_password'])) $this->db->set('user_password', md5($options['user_password']));

		if(isset($options['user_group_id'])) $this->db->set('user_group_id', $options['user_group_id']);
			
		if(isset($options['user_status'])) $this->db->set('user_status', $options['user_status']);
                
        if(isset($options['user_last_visited'])) $this->db->set('user_last_visited', $options['user_last_visited']);

		$this->db->where('user_id', $options['user_id']);
		
		$this->db->update('users');
		
		unset($options['user_password']);
		return $this->db->affected_rows();
	}
	
	
	function user_info($options = array())
	{
		// Qualification
		if(isset($options['user_id'])) $this->db->where('user_id', $options['user_id']);
		if(isset($options['user_email'])) $this->db->where('user_email', $options['user_email']);		
		if(isset($options['user_password'])) $this->db->where('user_password', $options['user_password']);	
		if(isset($options['user_status'])) $this->db->where('user_status', $options['user_status']);
        if(isset($options['user_group_id'])) $this->db->where('user_group_id', $options['user_group_id']);

		// limit / offset
		if(isset($options['limit']) && isset($options['offset'])) $this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit'])) $this->db->limit($options['limit']);
			
		// sort
		if(isset($options['sortBy']) && isset($options['sortDirection'])) $this->db->order_by($options['sortBy'], $options['sortDirection']);
			
		if(!isset($options['user_status'])) $this->db->where('user_status !=', '0');
		
		$query = $this->db->get("users");
		
		if(isset($options['count'])) return $query->num_rows();
		
		if(isset($options['user_id']) || isset($options['user_email'])) return $query->row(0);
		
		
		return $query->result();
	}
	
	
	function user_login($options = array())
	{
		// required values
		if(!$this->_required( array('user_email', 'user_password'), $options) ) return false;
		
		$user = $this->user_info(array('user_email' => $options['user_email'], 'user_password' => md5($options['user_password'])));
		
		if($user){
			$option = array();
			$option['user_id'] = $user->user_id;
			$option['user_last_visited'] = mdate('%Y-%m-%d %H:%i:%s');
			$this->user_update($option);
			
			$this->session->set_userdata('user_email', $user->user_email);
			$this->session->set_userdata('user_id', $user->user_id);
			$this->session->set_userdata('user_group_id', $user->user_group_id);
			
			return true;
        }
        
        $this->session->set_flashdata('error', 'Username and password do not match');
        return false;
	}
	
   
        function user_secure($options = array())
        {
            // required values
            if(!$this->_required( array('user_group_id'), $options)) return false;

            $userType = $this->session->userdata('user_group_id');

            if(is_array($options['user_group_id']))
            {
                foreach($options['user_group_id'] as $optionUserType)
                {
                    if($optionUserType == $userType) return true;
                }
            }
            else
            {
                if($userType == $options['user_group_id']) return true;
            }

            return false;
        }
        public function __destruct()
        {
        	unset($options);
        }
}